package com.jinwei.qunguanjia.web.tencent.push;

import com.tencent.xinge.XingeApp;
import com.tencent.xinge.bean.*;
import com.tencent.xinge.bean.ios.Alert;
import com.tencent.xinge.bean.ios.Aps;
import com.tencent.xinge.push.app.PushAppRequest;
import org.json.JSONObject;

import java.util.ArrayList;

public class PushController {
    public static void main(String[] args) {
        ArrayList<String> tokenList = new ArrayList<>();
        tokenList.add("0c8187e4d6cad7b0131e3e5a510caa5a9901");
        System.out.println(new PushController().pushTokenIos(tokenList));
    }

    // Android设备token推送
    public JSONObject pushTokenAndroid(ArrayList<String> tokenList) {
        XingeApp xingeApp = new XingeApp.Builder()
                .appId("1580018856")
                .secretKey("37082343881626e8031acf5cf79b0650")
                .domainUrl("https://api.tpns.sh.tencent.com/")
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        // 选择推送目标类型
        pushAppRequest.setAudience_type(AudienceType.token);
        // 消息类型：通知栏或透传消息
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        // 推送标题
        message.setTitle("title");
        // 推送内容
        message.setContent("content");
        pushAppRequest.setMessage(message);
        MessageAndroid messageAndroid = new MessageAndroid();
        message.setAndroid(messageAndroid);
        // 设置token列表
        pushAppRequest.setToken_list(tokenList);
        return xingeApp.pushApp(pushAppRequest);
    }

    // iOS设备token推送
    public JSONObject pushTokenIos(ArrayList<String> tokenList){
        XingeApp xingeApp = new XingeApp.Builder()
                .appId("1680018856")
                .secretKey("ba7f54398e345cf1aecf2c9279615921")
                .domainUrl("https://api.tpns.sh.tencent.com/")
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        // 选择推送目标类型
        pushAppRequest.setAudience_type(AudienceType.token);
        // 指定推送环境
        pushAppRequest.setEnvironment(Environment.valueOf("product"));
        // 消息类型（通知栏/透传消息）
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        // 推送标题
        message.setTitle("通知");
        // 推送内容
        message.setContent("您有新的群待领取");
        MessageIOS messageIOS = new MessageIOS();
        Alert alert = new Alert();
        Aps aps = new Aps();
        // 通知的内容属性
        aps.setAlert(alert);
        // 通知属性
        messageIOS.setAps(aps);
        // 推送平台（iOS/Android）
        message.setIos(messageIOS);
        pushAppRequest.setMessage(message);
        // 设置token列表
        pushAppRequest.setToken_list(tokenList);
        return xingeApp.pushApp(pushAppRequest);
    }

}
